package com.rem.rabbitmq.ee.Aquick;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

/**
 * 消费者
 *
 * @author Rem
 * @date 2021-12-12
 */

public class Consumer {


    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("106.14.72.13");
        factory.setPort(5672);
        factory.setUsername("rem");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        try {

            //消费回调
            DeliverCallback deliverCallback = (consumerTag, message) -> {
                System.err.println("接收到的消息：" + new String(message.getBody()));
            };

            //取消消费回到 如果在消费的时候队列被删除了
            CancelCallback cancelCallback = consumerTag -> {
                System.err.println("消费消息被中断");
            };
            /**
             * queue – 队列的名称
             * autoAck – 如果服务器应该考虑消息一旦发送就确认为真； 如果服务器应该期待显式确认，则为 false
             * DeliverCallback – 传递消息时的回调
             * cancelCallback – 消费者取消时的回调
             */
            channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
        } finally {
            channel.close();
            connection.close();
        }

    }
}
